//
// Created by YxMin on 2024/7/5.
//
#include <network.h>
#include <cstdio>
#include <procedure.h>
#include <netdata.h>
#include <queue>
#include <UI.h>
#include <tinycthread.h>
#include "../thread/ABQueue.h"
GamePack parseGamePack(const char* &recBuffer, bool copyData = false); //与roompack不同，由于gamepack是存在消息队列里延迟处理的，因此data部分需要复制而不能用调用方提供的buffer指针
ABQueue<GamePack> PackFromServerQueue;

void ParseAndHandleGamePackFromServer(const char* recBuffer)//recBuffer字符串的内容不可修改，
{
    do {
        GamePack pack = parseGamePack(recBuffer, true);//注意处理粘包
        if (pack.packId == -1)
            continue;
        if (pack.packId != 1)
            printf("[C](GP)eid:%d, pid:%d, tick:%d, dataLength:%d, data:%s\n",pack.packId,pack.playerId,pack.tick, pack.dataLength,pack.data);
        if (!GameStarted) {
            SetTick(pack.tick);
            GameStarted = true;
        }
        PackFromServerQueue.enqueue(pack);//push时会复制pack本身
    } while (*recBuffer != 0);
}


void ParseAndHandleGamePackFromPlayer(ClientMessage* msg)
{
    char buffer[128];
    do {
        GamePack pack = parseGamePack(msg->msg);//注意处理粘包
        if (pack.packId == -1)
            continue;
        if (pack.packId != 1)
            printf("[S](GP)<-(%d) eid:%d, pid:%d, tick:%d, dataLength:%d, data:%s\n",msg->id,pack.packId,pack.playerId,pack.tick, pack.dataLength,pack.data);
        ExecutePlayerPackage(pack,buffer);
    } while (*(msg->msg) != 0);
}

//结束时recBuffer指向该包最后一个字符(>)的下一个字符
GamePack parseGamePack(const char* &recBuffer, bool copyData) { //recBuffer来自network中的线程
    GamePack pack = {-1,-1,-1, nullptr,0};
    const char *start,*stop;
    start = recBuffer;
    while(*start !=0) {
        stop = start+1;
        if (*start == '<' && start[1] == '(' && start[2] == '[' && start[3] != 0) {
            stop = start + 4;
            while(*stop != 0) {
                if (*stop == ']' && stop[1] ==')' && stop[2] =='>') {
                    pack.packId = (int)(start[4] & 0x0f);
                    pack.playerId = (int)(start[3] & 0x0f);
                    pack.tick = 0;
                    int bit = 10000000;
                    int i = 5;
                    while (bit != 0) {
                        pack.tick += (int)(start[i] & 0x0f) * bit;
                        i++;
                        bit /= 10;
                    }
                    start += 14; //此时start指向data第一位，而stop指向]
                    pack.data = start;
                    pack.dataLength = (int)(stop - start);
                    if (copyData) {
                        char *newBuffer = new char[64];
                        pack.data = newBuffer;
                        while(start != stop) {
                            *newBuffer = *start;
                            newBuffer++;
                            start++;
                        }
                    }
                    stop += 3;//stop指到>后一位
                    recBuffer = stop;
                    return pack;
                }
                stop++;
            }
        }
        start++;
    }
    recBuffer = stop;
    return pack;
}